<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
    <style>
        body{
            background-image: url("image/background_Login.jpg");
            background-size: 100%;
        }
    </style>
</head>
<body>
    <!--   导航栏   -->
    <div class="nav">
        <img src="image/person_Blog.png" alt="">
        <span>我的博客系统</span>
        <div class="spacer"></div>
        <a href="list.html">主页</a>
        <a href="edit.html">写博客</a>
        <a href="enroll.html">注册</a>
    </div>

    <div class="login-container">
        <div class="login-dialog">
            <h3>登录</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" id="username" onkeyup="this.value=this.value.replace(/[, ]/g,'')">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <input type="submit" value="登录" id="submit">
            </div>
        </div>
    </div>


    <script src="jquery.js"></script>
    <script>
        let submit = document.querySelector("#submit");
        submit.onclick = function () {
            let username = document.querySelector("#username");
            let password = document.querySelector("#password");
            let user = {
                userName: username.value,
                password: password.value,
            }
            $.ajax({
                type: 'post',
                url: 'login',
                contentType: "application/json; charset=utf8",
                data: JSON.stringify(user),
                success: function (body) {
                    if(body == 'fail'){
                        alert("用户名或密码错误 请重试!");
                        location = 'login.html';
                    }else if(body == "success"){
                        location = 'list.html';
                    }
                },
                error: function () {
                    alert("网络连接错误 请重试!");
                    location.assign("login.html")
                }
            })
        }
    </script>
</body>
</html>